#include <stdio.h>
#include <stdlib.h>

// 定义红黑树结点颜色
enum Color
{
    RED,
    BLACK
};

// 定义红黑树结点结构
struct RBNode
{
    int data;              // 结点数据
    struct RBNode *left;   // 左子树指针
    struct RBNode *right;  // 右子树指针
    struct RBNode *parent; // 父结点指针
    enum Color color;      // 结点颜色
};

// 创建新结点（默认为红色）
struct RBNode *newRBNode(int data)
{
    struct RBNode *node = (struct RBNode *)malloc(sizeof(struct RBNode));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
    node->parent = NULL;
    node->color = RED; // 新结点初始为红色
    return node;
}

// 左旋转
void leftRotate(struct RBNode **root, struct RBNode *x)
{
    struct RBNode *y = x->right;
    x->right = y->left;

    if (y->left != NULL)
        y->left->parent = x;

    y->parent = x->parent;

    if (x->parent == NULL)
        *root = y;
    else if (x == x->parent->left)
        x->parent->left = y;
    else
        x->parent->right = y;

    y->left = x;
    x->parent = y;
}

// 右旋转
void rightRotate(struct RBNode **root, struct RBNode *y)
{
    struct RBNode *x = y->left;
    y->left = x->right;

    if (x->right != NULL)
        x->right->parent = y;

    x->parent = y->parent;

    if (y->parent == NULL)
        *root = x;
    else if (y == y->parent->left)
        y->parent->left = x;
    else
        y->parent->right = x;

    x->right = y;
    y->parent = x;
}

// 插入后修复红黑树性质
void fixInsert(struct RBNode **root, struct RBNode *z)
{
    while (z != *root && z->parent->color == RED)
    {
        if (z->parent == z->parent->parent->left)
        {
            struct RBNode *y = z->parent->parent->right;

            // 情况1：叔叔结点是红色
            if (y != NULL && y->color == RED)
            {
                z->parent->color = BLACK;
                y->color = BLACK;
                z->parent->parent->color = RED;
                z = z->parent->parent;
            }
            else
            {
                // 情况2：叔叔结点是黑色，且z是右孩子
                if (z == z->parent->right)
                {
                    z = z->parent;
                    leftRotate(root, z);
                }

                // 情况3：叔叔结点是黑色，且z是左孩子
                z->parent->color = BLACK;
                z->parent->parent->color = RED;
                rightRotate(root, z->parent->parent);
            }
        }
        else
        { // 对称情况（父结点是祖父的右孩子）
            struct RBNode *y = z->parent->parent->left;

            // 情况1：叔叔结点是红色
            if (y != NULL && y->color == RED)
            {
                z->parent->color = BLACK;
                y->color = BLACK;
                z->parent->parent->color = RED;
                z = z->parent->parent;
            }
            else
            {
                // 情况2：叔叔结点是黑色，且z是左孩子
                if (z == z->parent->left)
                {
                    z = z->parent;
                    rightRotate(root, z);
                }

                // 情况3：叔叔结点是黑色，且z是右孩子
                z->parent->color = BLACK;
                z->parent->parent->color = RED;
                leftRotate(root, z->parent->parent);
            }
        }
    }
    (*root)->color = BLACK; // 确保根结点始终为黑色
}

// 插入结点到红黑树
void insert(struct RBNode **root, int data)
{
    // 创建新结点
    struct RBNode *z = newRBNode(data);
    struct RBNode *y = NULL;
    struct RBNode *x = *root;

    // 标准BST插入
    while (x != NULL)
    {
        y = x;
        if (z->data < x->data)
            x = x->left;
        else
            x = x->right;
    }

    z->parent = y;

    if (y == NULL)
        *root = z; // 树为空
    else if (z->data < y->data)
        y->left = z;
    else
        y->right = z;

    // 修复红黑树性质
    fixInsert(root, z);
}

// 中序遍历（升序输出）
void inOrder(struct RBNode *root)
{
    if (root != NULL)
    {
        inOrder(root->left);
        printf("%d(%s) ", root->data, root->color == RED ? "RED" : "BLACK");
        inOrder(root->right);
    }
}

// 主函数测试
int main()
{
    struct RBNode *root = NULL;

    // 测试插入序列
    int arr[] = {7, 3, 18, 10, 22, 8, 11, 26};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("Inserting values: ");
    for (int i = 0; i < n; i++)
    {
        printf("%d ", arr[i]);
        insert(&root, arr[i]);
    }
    printf("\n");

    // 打印中序遍历结果
    printf("Inorder traversal of the constructed Red-Black tree: ");
    inOrder(root);
    printf("\n");

    return 0;
}